/*
 * Copyright 2011 Vlad Goldman.  All rights reserved.
  
   This file is part of GooOutSync - Google Outlook Sync application
  
    Project page: http://goooutsync.googlecode.com

    GooOutSync is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    GooOutSync is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with GooOutSync.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.vgold.goooutsync;

import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;

public class GooOutProxy {
	private static Logger logger = Logger.getLogger(GooOutProxy.class);

	public enum ProxyMode {
		NONE, MANUAL, SYSTEM
	}

	ProxyMode currentMode = ProxyMode.NONE;

	public void autodetect() {

	}

	public static void setManualProxy(String host, String port) {
		logger.info("Setting manual proxy settings: " + host + ":" + port);
		System.setProperty("http.proxyHost", host);
		System.setProperty("http.proxyPort", port);
		System.setProperty("https.proxyHost", host);
		System.setProperty("https.proxyPort", port);
	}

	public static void setSystemMode() {
		System.setProperty("java.net.useSystemProxies", "true");
		List l = null;
		try {
			l = ProxySelector.getDefault().select(
					new URI("http://www.google.com/"));
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		if (l != null) {
			for (Iterator iter = l.iterator(); iter.hasNext();) {
				java.net.Proxy proxy = (java.net.Proxy) iter.next();
				System.out.println("proxy hostname : " + proxy.type());

				InetSocketAddress addr = (InetSocketAddress) proxy.address();
				if (addr == null) {
					System.out.println("No Proxy");
				} else {
					System.out
							.println("proxy hostname : " + addr.getHostName());
					System.setProperty("http.proxyHost", addr.getHostName());
					System.out.println("proxy port : " + addr.getPort());
					System.setProperty("http.proxyPort",
							Integer.toString(addr.getPort()));
				}
			}
		}

	}
}
